<?php
class BaseEntity
{
    /**
     * Class constructor.
     * 
     * @params array|null $vars instance variables
     * @return void
     */
    public function __construct(array $vars = null)
    {
        if (null !== $vars) {
            $this->declareInstanceVars($vars);
        }
    }

    /**
     * Declare instance variables.
     *
     * @param array $vars
     * @return void
     */
    public function declareInstanceVars(array $vars)
    {
        foreach ($vars as $name => $value) {
            if (! property_exists($this, $name)) {
                continue;
            }
            $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
            if (method_exists($this, $method)) {
                $this->$method($value);
            } else {
                $this->$name = $value;
            }
        }
    }
    
    /**
     * Return an associative array containing all of object propertiers 
     * and its values. 
     *
     * @return array
     */
    public function toArray()
    {
        return get_object_vars($this);
    }

    /**
     * Assign value to variable id.
     * 
     * @return void
     * @throws RuntimeException
     */
    public function setId($id)
    {
        if (! property_exists($this, 'id')) {
            throw new RuntimeException('Instance variable "id" not declared.');
        } else if (! is_numeric($id)) {
            throw new RuntimeException('Incorrect integer value for "id"');
        }
        $this->id = (int) $id;
    }
    
    /**
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}
?>